package com.itic.mall.system.users.pointslog.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.type.OperateType;
import com.itic.appbase.framework.type.PointsChangeReason;
import com.itic.mall.system.inter.users.pointslog.persistence.dao.UsersPointslogDao;
import com.itic.mall.system.inter.users.pointslog.persistence.model.UsersPointslog;
import com.itic.mall.system.inter.users.pointslog.persistence.model.UsersPointslogExample;
import com.itic.mall.system.inter.users.pointslog.persistence.model.UsersPointslogExample.Criteria;
import com.itic.mall.system.inter.users.usersmall.persistence.model.UsersMall;
import com.itic.mall.system.users.usersmall.service.UsersMallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class UsersPointslogService
     extends CrudService<UsersPointslogDao,UsersPointslog,UsersPointslogExample> {
    @Autowired
    private UsersPointslogService usersPointslogService;
    
    @Autowired
    private UsersMallService usersMallService;

    public UsersPointslog getByUserId(String userId) {
    	UsersPointslogExample example = new UsersPointslogExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	List<UsersPointslog> usersPointslogs = this.usersPointslogService.list(example);
    	if(usersPointslogs.isEmpty())
    		return null;
    	else
    		return usersPointslogs.get(0);
    }
    public UsersPointslog getLastPointsLogbyUserId(String userId) {
    	UsersPointslogExample example = new UsersPointslogExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	example.setOrderByClause("create_time desc");
    	List<UsersPointslog> usersPointslogs = this.usersPointslogService.list(example);
    	if(usersPointslogs.isEmpty())
    		return null;
    	else
    		return usersPointslogs.get(0);
    }
    
    
    public String dealUsersPointsLog(String userId,OperateType operate,int count,String orderNo) {
    		//获取最后一次积分日志 
    		UsersMall usersMall = this.usersMallService.get(userId);
    		int oldPoint = usersMall.getJifeng()==null?0:usersMall.getJifeng();
    		UsersPointslog usersPoints = new UsersPointslog();
    		switch (operate) {
				case add:
					//TODO 添加算法
					oldPoint = oldPoint + count;
					usersPoints.setType("001");
					usersPoints.setChangeReason(PointsChangeReason.add.getType());
					break;
				case sub:
					oldPoint = oldPoint - count;
					oldPoint = oldPoint<=0?0:oldPoint;
					usersPoints.setType("002");
					usersPoints.setChangeReason(PointsChangeReason.reduce.getType());
					break;
				default:
					break;
			}
    		// 添加记录
    		usersPoints.setPoints(oldPoint);
    		usersPoints.setUserId(userId);
    		usersPoints.setOrderNo(orderNo);
    		String result = this.add(usersPoints);
    		usersMall.setJifeng(oldPoint);
    		result = this.usersMallService.edit(usersMall);
    	return result;
    }
}